package algocasts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class P11_Permute {

    void permuteRec(List<Integer> nums, int start, List<List<Integer>> result) {
        if (start == nums.size()) {
            result.add(new ArrayList<>(nums));
        } else {
            for (int i = start; i < nums.size(); i++) {
                Collections.swap(nums, i, start);
                permuteRec(nums, start + 1, result);
                Collections.swap(nums, i, start);
            }
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) return new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        for (int num : nums) list.add(num);
        permuteRec(list, 0, result);
        return result;
    }

    public static void main(String[] args) {
        P11_Permute solution = new P11_Permute();
        int []nums = {0,1,2};
        List<List<Integer>> lists = solution.permute(nums);
        for (List<Integer> list : lists) {
            System.out.println(list.toString());
        }
    }
}
